from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import json
import time
import os
import sys
import datetime
import random
import requests

class WeiboAutoReposter:
    def __init__(self):

        self.driver = self._init_driver()
        self.target_user = "大皖新闻"  # 目标用户名

        self.api_url = "https://api.siliconflow.cn/v1/chat/completions"
        self.api_key = "sk-nbnpftafdbxqfttaedidmqpwevinkvixnucpudanuykywflx"
        self.max_retries = 3
        # 从文件加载历史记录
        self.processed_mids = set()
        if os.path.exists("processed.log"):
            with open("processed.log", "r") as f:
                self.processed_mids = set(f.read().splitlines())

        # 凌晨减少检查频率
        hour = datetime.datetime.now().hour
        if 0 <= hour < 7:  
            self.check_interval = (25200,25400)  # 7小时
        else:
            self.check_interval = (360, 600)
        self.last_mid = None  # 最后转发的微博ID
        self.cookie_file = "weibo_cookies.json"

        # 初始化cookies
        if os.path.exists(self.cookie_file):
            self.login_with_cookies()
        else:
            self.manual_login()
            self.save_cookies()
    
    # 配置浏览器选项
    def _init_driver(self):
     
        current_dir = os.path.dirname(os.path.abspath(__file__))
        options = Options()
        options.add_argument('--no-sandbox')  # 禁用沙箱
        options.add_argument('--disable-dev-shm-usage')  # 禁用共享内存
        options.add_argument('--force-device-scale-factor=1')
        chrome_dir = os.path.join(current_dir, "chrome")
        options.binary_location =os.path.join(chrome_dir, "chrome.exe")
        driver_path = os.path.join(current_dir, "drivers", "chromedriver.exe")
        service = Service(executable_path=driver_path)
        driver = webdriver.Chrome(service=service, options=options)
        return driver
    
    def save_cookies(self):
        with open(self.cookie_file, 'w') as f:
            json.dump(self.driver.get_cookies(), f)

    def load_cookies(self):
        self.driver.get("https://weibo.com")
        with open(self.cookie_file, 'r') as f:
            cookies = json.load(f)
            for cookie in cookies:
                self.driver.add_cookie(cookie)
        self.driver.refresh()

    def manual_login(self):
        self.driver.get("https://weibo.com/login.php")
        input("请手动完成登录后按回车继续...")

    def login_with_cookies(self):
        try:
            self.load_cookies()
            # 验证登录是否成功
            WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located((By.CLASS_NAME, "gn_name"))
            )
            print("登录成功")
        except Exception as e:
            print("Cookie登录失败，请手动登录")
            self.manual_login()
            self.save_cookies()

    def get_target_weibo(self):
        # 跳转到目标用户主页
        self.driver.get(f"https://weibo.com/n/{self.target_user}")
        
        try:               
            # 等待主容器加载
            WebDriverWait(self.driver, 15).until(
                EC.presence_of_element_located((By.XPATH, '//div[@class="container"]//div[contains(@class,"wbpro-scroller-item") and @data-index]'))
            )

            # 精准定位
            weibo_list = self.driver.find_elements(By.XPATH, '//div[@class="container"]//div[contains(@class,"wbpro-scroller-item") and @data-index]')
            print(f"成功获取到 {len(weibo_list)} 条微博")
            return weibo_list
        
        except Exception as e:
            print("元素定位失败:", str(e))
            return []


    def repost_weibo(self, weibo_item):
        try:
            # 定位评论按钮
            repost_btn = weibo_item.find_element(By.XPATH, './/i[@title="转发"]')
            
            # 滚动到元素可见（核心修正）
            self.driver.execute_script(
                "arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'});"
                "window.scrollBy(0, -50);", 
                repost_btn
            )
            
            # 显式等待元素可点击
            WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, './/i[@title="转发"]'))
            )
            
            # 使用ActionChains模拟人类操作
            ActionChains(self.driver).move_to_element(repost_btn).pause(3.4).click().perform()
            print("点击转发按钮")

            WebDriverWait(self.driver, 15).until(
                EC.presence_of_element_located((By.XPATH, '//textarea[@placeholder="说说分享心得"]'))
            )            
          
            # 使用ActionChains模拟人类操作
            comfpost_btn = self.driver.find_element(By.XPATH, '//button[.//span[text()="转发"]]')
            ActionChains(self.driver).move_to_element(comfpost_btn).pause(2.1).click().perform()
       
            print("转发成功")

            # 点赞
            time.sleep(2)
            weibo_item.find_element(By.XPATH, './/button[@title="赞"]').click()
            print("点赞成功")

            return True
        except Exception as e:
            print("转发失败:", str(e))
            return False
    def run(self):
        try:
            while True:
                print("开始检查新微博...")
                weibo_list = self.get_target_weibo()
                
                if weibo_list:
                    # 逆序处理：从旧到新，避免重复处理最新微博
                    for weibo in reversed(weibo_list):
                        try:
                            # 更健壮的MID提取方式
                            weibo_link = weibo.find_element(By.XPATH, './/a[contains(@href, "weibo.com/")]')
                            href = weibo_link.get_attribute("href")
                            current_mid = href.split("/")[-1].split("?")[0]  # 处理带参数的URL
                        except Exception as e:
                            print(f"提取MID失败: {str(e)}")
                            continue  # 跳过无法处理的微博

                        if current_mid not in self.processed_mids:
                            print(f"发现未转发点赞的新微博 {current_mid}")
                            if self.repost_weibo(weibo):
                                self.processed_mids.add(current_mid)
                                print(f"{datetime.datetime.now()}成功记录已转发ID: {current_mid}")
                                # 可选：写入日志文件
                                with open("processed.log", "a") as f:
                                    f.write(f"{current_mid}\n")
                                time.sleep(60)
                            else:
                                print(f"跳过转发点赞失败的微博: {current_mid}")
                        else:
                            print(f"微博ID: {current_mid}已处理...")
                else:
                    print("没有检测到新的微博")

                wait_time=random.randint(*self.check_interval)
                print(f"等待{wait_time}秒后再次检查...")
                time.sleep(wait_time)
                
        finally:
            self.driver.quit()

if __name__ == "__main__":
    bot = WeiboAutoReposter()
    bot.run()
